package edu.upc.galaxy.controller;

import edu.upc.galaxy.entity.Credential;
import edu.upc.galaxy.entity.Comentario;
import edu.upc.galaxy.service.ComentarioService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author ydb
 */
@Controller("ComentarioController")
public class ComentarioController {

    @Autowired
    private ComentarioService comentarioService;
    private static Logger log = LoggerFactory.getLogger(ComentarioController.class);

    @RequestMapping("/Comentario/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("Comentario/index");
        List<Comentario> comentario = comentarioService.buscarTodos();
        mav.addObject("Comentario", comentario);
        return mav;
    }


    
    @RequestMapping("/Comentario/buscar")
    public ModelAndView buscar() {
        ModelAndView mav = new ModelAndView("Comentario/buscar");
        List<Comentario> comentario = comentarioService.buscarTodos();
        mav.addObject("Comentario", comentario);
        return mav;
    }

  /*  @RequestMapping("/canchas/alquilar")
    public ModelAndView buscar() {
        ModelAndView mav = new ModelAndView("canchas/alquilar");
        List<Cancha> canchas = canchaService.buscarCanchaDisponible();
        mav.addObject("canchas", canchas);
        return mav;
    }    
    */
    @RequestMapping(value = "/Comentario/new", method = RequestMethod.GET)
    public ModelAndView newComentario() {
        ModelAndView mav = new ModelAndView("Comentario/new");
        Comentario comentario = new Comentario();
        mav.getModelMap().put("Comentario", comentario);
        return mav;
    }

    @RequestMapping(value = "/Comentario/new", method = RequestMethod.POST)
    public String createCancha(@ModelAttribute("Comentario")Comentario comentario, SessionStatus status) {    
        comentarioService.insertar(comentario);
        status.setComplete();

        return "redirect:/pages/Comentario/index";
        
    }
    
    @RequestMapping(value = "/Comentario/edit", method = RequestMethod.GET)
    public ModelAndView editComentario(@RequestParam("id")Integer id) {    
        ModelAndView mav = new ModelAndView("Comentario/edit");
        Comentario comentario = comentarioService.buscar(id);
        mav.getModelMap().put("Comentario", comentario);
        return mav;        
    }
    
    @RequestMapping(value="/Comentario/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("Comentario") Comentario comentario, SessionStatus status) {
        canchaService.actualizar(comentario);
        status.setComplete();
        return "redirect:/pages/Comentario/index";
    }
    
    @RequestMapping("/Comentario/delete")
    public ModelAndView delete(@RequestParam("id")Integer id)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/Comentario/index");
        Comentario comentario = comentarioService.buscar(id);
        comentarioService.eliminar(comentario);
        return mav;
    }
    
    @RequestMapping("/Comentario/login")
    public ModelAndView login() {
        ModelAndView mav = new ModelAndView("Comentario/login");
        Credential credential = new Credential();
        mav.getModelMap().put("credential", credential);
        return mav;
    }    
    
}
